/**
 * RenderJSONEx is a class extending the Result class to allows processing the GSON
 * @Expose annotation in the rendered objects.
 * 
 * By default, all the RenderJSON framework functions ignore this annotation: they render
 * all the object fields. Since we need skip some field during the rendering, the
 * excludeFieldsWithoutExposeAnnotation() must be called on the GsonBuilder object.
 * 
 * This code is a copy/paste of the original RenderJSON function.
 * 
 * @author Eric LEON
 * @version 1.0.0
 */
package results;

import java.lang.reflect.Type;

import play.exceptions.UnexpectedException;
import play.mvc.Http.Request;
import play.mvc.Http.Response;
import play.mvc.results.Result;

import com.google.gson.GsonBuilder;

public class RenderJSONEx extends Result {

    String json;

    public RenderJSONEx(Object o) {
	    GsonBuilder gson = new GsonBuilder();
	    gson.excludeFieldsWithoutExposeAnnotation();
	    json = gson.create().toJson(o);
    }   

    public void apply(Request request, Response response) {
        try {
            setContentTypeIfNotSet(response, "application/json; charset=utf-8");
            response.out.write(json.getBytes("utf-8"));
        } catch (Exception e) {
            throw new UnexpectedException(e);
        }
    }
}
